package com.crm.service.impl.contactManager;


import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.dao.mapper.contactManager.ContactInformationManyPeopleMapper;

import com.crm.dao.mapper.contactManager.ContactInformationManyPeopleNameMapper;
import com.crm.dao.mapper.customMapper.contactManagerCustomMapper.ContactInformationManyPeopleCustomMapper;
import com.crm.model.entity.contactManager.*;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.qo.contactManager.ContactInformationManyPeopleQO;
import com.crm.model.vo.contactManager.*;
import com.crm.service.contactManager.ContactInformationManyPeopleDbService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 联系人管理信息
 */
@Service
public class ContactInformationManyPeopleDbServiceImpl implements ContactInformationManyPeopleDbService {


    @Autowired
    ContactInformationManyPeopleCustomMapper contactInformationManyPeopleCustomMapper;

    @Autowired
    ContactInformationManyPeopleMapper contactInformationManyPeopleMapper;

    @Autowired
    ContactInformationManyPeopleNameMapper contactInformationManyPeopleNameMapper;

    /**
     * 联系联系方式一机多用
     *
     * @param dateUnit 1本周  2 本月
     * @param type  1超管 2普通管理员 3用户
     * @param contactInformationManyPeopleQO 查询条件
     * @return
     */
    @Override
    public List<ContactInformationManyPeopleVO> contactInformationManyPeopleList(ContactInformationManyPeopleQO contactInformationManyPeopleQO, Integer dateUnit, int type) {
        return contactInformationManyPeopleCustomMapper.contactInformationManyPeopleList(contactInformationManyPeopleQO,dateUnit,type);
    }

    /**
     * 新增联系方式人
     * @param contactInformationManyPeople
     * @return
     */
    @Override
    public Boolean addContactInformationManyPeople(ContactInformationManyPeople contactInformationManyPeople) {
        Boolean result = false;
        int num = contactInformationManyPeopleMapper.insertSelective(contactInformationManyPeople);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 联系人名称管理信息
     * @param contactInformationManyPeopleId 联系方式一机多用id
     * @param bs 0显示未废止的  1显示全部的
     * @return
     */
    @Override
    public List<ContactInformationManyPeopleName> contactInformationManyPeopleNameList(String contactInformationManyPeopleId,String bs) {
        ContactInformationManyPeopleNameExample example = new ContactInformationManyPeopleNameExample();
        ContactInformationManyPeopleNameExample.Criteria criteria = example.createCriteria();
        example.setOrderByClause("create_tm desc");
        criteria.andContactInformationManyPeopleIdEqualTo(contactInformationManyPeopleId);
        if("0".equals(bs)) {
            criteria.andAbolishStateEqualTo(1);
        }
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<ContactInformationManyPeopleName> list = contactInformationManyPeopleNameMapper.selectByExample(example);
        return list;
    }

    /**
     * 新增联系人名称
     * @param name
     * @return
     */
    @Override
    public Boolean addContactInformationManyPeopleName(ContactInformationManyPeopleName name) {
        Boolean result = false;
        int num = contactInformationManyPeopleNameMapper.insertSelective(name);
        if (num > 0) {
            result = true;
        }
        return result;
    }

    /**
     * 通过id查询联系方式一用多机
     * @param contactInformationManyPeopleId 联系方式表id
     * @return
     */
    @Override
    public ContactInformationManyPeople selectContactInformationManyPeople(String contactInformationManyPeopleId) {
        return contactInformationManyPeopleMapper.selectByPrimaryKey(contactInformationManyPeopleId);
    }

    /**
     * 修改联系名称表联系人姓名
     * @param name
     * @return
     */
    @Override
    public Boolean updateContactInformationManyPeopleName(ContactInformationManyPeopleName name) {
        Boolean result = false;
        int num = contactInformationManyPeopleNameMapper.updateByPrimaryKeySelective(name);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 联系方式一机多用表联系人姓名
     * @param contactInformationManyPeople
     */
    @Override
    public Boolean updateContactInformationManyPeople(ContactInformationManyPeople contactInformationManyPeople) {
        Boolean result = false;
        int num = contactInformationManyPeopleMapper.updateByPrimaryKeySelective(contactInformationManyPeople);
        if(num>0){
            result = true;
        }
        return result;
    }

    /**
     * 通过联系方式和用户id查询联系方式是否重复
     * @param contactInformation 联系方式
     * @param accountId 用户id
     * @return
     */
    @Override
    public ContactInformationManyPeople selectManyPeopleByPhoneAndAccountId(String contactInformation, String accountId) {
        ContactInformationManyPeopleExample example = new ContactInformationManyPeopleExample();
        ContactInformationManyPeopleExample.Criteria criteria = example.createCriteria();
        criteria.andContactInformationEqualTo(contactInformation);
        criteria.andAccountIdEqualTo(accountId);
        criteria.andDeleteStatusEqualTo(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
        List<ContactInformationManyPeople> list = contactInformationManyPeopleMapper.selectByExample(example);
        if(list != null && list.size() > 0){
            return list.get(0);
        }else{
            return null;
        }

    }
}
